package com.salim.leetcode.$14;

import lombok.extern.slf4j.Slf4j;

/**
 * 寻找数组中的最长相同前缀
 * 题目 https://leetcode.com/problems/longest-common-prefix/
 * 答案 https://leetcode.com/articles/longest-common-prefix/
 */
@Slf4j
public class LongestCommonPrefix {

    /**
     * 方法1 水平扫描
     * @param strs
     * @return
     */
    public String longestCommonPrefix(String[] strs) {
        if(strs.length==0){
            return "";
        }
        String longest= strs[0];
        for(int i=1;i<strs.length;i++){
            //如果目标长度比当前最长长度小 将当前最长长度截取
            if(strs[i].length()<longest.length()){
                longest = longest.substring(0,strs[i].length());
            }
            //找到最长的相同前缀
            for(int j=longest.length();j>0;j--){
                if(longest.substring(0,j).equals(strs[i].substring(0,j))){
                    break;
                }else{
                    if(j>1){
                        longest = longest.substring(0,j-1);
                    }else{
                        return "";
                    }
                }
            }
        }
        return longest;
    }

    public static void main(String[] args){
        LongestCommonPrefix l = new LongestCommonPrefix();
        String[] arg1 = {"flower","flow","flight"};
        String[] arg2 = {"dog","racecar","car"};
        String[] arg3 = {};
        String[] arg4 = {"dog"};
        log.info("longestCommonPrefix输出结果1：{}",l.longestCommonPrefix(arg1));
        log.info("longestCommonPrefix输出结果2：{}",l.longestCommonPrefix(arg2));
        log.info("longestCommonPrefix输出结果3：{}",l.longestCommonPrefix(arg3));
        log.info("longestCommonPrefix输出结果4：{}",l.longestCommonPrefix(arg4));
    }


    /**
     * 方法2 垂直扫描
     * 扫描每个字符串同一个index的字符
     * 第一轮
     * abc abcd acbc
     * ↑   ↑    ↑
     * 第二轮
     * abc abcd acbc
     *  ↑   ↑    ↑
     * 当数组末尾存在很短的字符串时
     * 该方法比1快
     * @param strs
     * @return
     */
    public String verticalScanning(String[] strs){
        if (strs == null || strs.length == 0) return "";
        for (int i = 0; i < strs[0].length() ; i++){
            char c = strs[0].charAt(i);
            for (int j = 1; j < strs.length; j ++) {
                if (i == strs[j].length() || strs[j].charAt(i) != c)
                    return strs[0].substring(0, i);
            }
        }
        return strs[0];
    }


    /**
     * 方法3 方法1的分治法版本
     * @param strs
     * @return
     */
    public String divideAndConquer(String[] strs){
        if (strs == null || strs.length == 0) return "";
        return divideAndConquer(strs, 0 , strs.length - 1);
    }
    private String divideAndConquer(String[] strs, int l, int r) {
        if (l == r) {
            return strs[l];
        }
        else {
            int mid = (l + r)/2;
            String lcpLeft = divideAndConquer(strs, l , mid);
            String lcpRight = divideAndConquer(strs, mid + 1,r);
            return commonPrefix(lcpLeft, lcpRight);
        }
    }
    String commonPrefix(String left,String right) {
        int min = Math.min(left.length(), right.length());
        for (int i = 0; i < min; i++) {
            if ( left.charAt(i) != right.charAt(i) )
                return left.substring(0, i);
        }
        return left.substring(0, min);
    }

    /**
     * 方法4 二分查找
     * 建议看答案好理解一点
     * @param strs
     * @return
     */
    public String binarySearch(String[] strs){
        if (strs == null || strs.length == 0)
            return "";
        int minLen = Integer.MAX_VALUE;
        for (String str : strs)
            minLen = Math.min(minLen, str.length());
        int low = 1;
        int high = minLen;
        while (low <= high) {
            int middle = (low + high) / 2;
            if (isCommonPrefix(strs, middle))
                low = middle + 1;
            else
                high = middle - 1;
        }
        return strs[0].substring(0, (low + high) / 2);
    }

    private boolean isCommonPrefix(String[] strs, int len){
        String str1 = strs[0].substring(0,len);
        for (int i = 1; i < strs.length; i++)
            if (!strs[i].startsWith(str1))
                return false;
        return true;
    }

}
